package com.fili.gui.wizards;

import com.fili.gui.WizardFrame;
import com.fili.gui.Params;

import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 18, 2006
 * Time: 2:20:58 PM
 * Revision: 0
 */
public class WizardUser extends WizardPanel implements ActionListener, DocumentListener, ChangeListener
{
    private JComboBox cbRepo;
    private JTextField edUser;
    private JPasswordField edPass;
    private JCheckBox cbAnonymous;

    public WizardUser(WizardFrame wizardFrame)
    {
        super(wizardFrame);

        setLayout(null);
        JLabel lbl = new JLabel("Repository: ");
        lbl.setBounds(10, 10, 100, 15);
        add(lbl);

        cbRepo = new JComboBox(Params.getParams().svnRepo);
        cbRepo.setEditable(true);
        cbRepo.setBounds(10, 30, 300, 20);
        add(cbRepo);

        cbAnonymous = new JCheckBox("Anonymous", !Params.getParams().svnAuth);
        cbAnonymous.setBounds(10, 60, 100, 15);
        add(cbAnonymous);
        cbAnonymous.addActionListener(this);
        cbAnonymous.addChangeListener(this);

        lbl = new JLabel("Username:");
        lbl.setBounds(30, 80, 100, 15);
        add(lbl);

        edUser = new JTextField(Params.getParams().svnUser);
        edUser.setBounds(30, 100, 120, 20);
        edUser.getDocument().addDocumentListener(this);
        add(edUser);

        lbl = new JLabel("Password:");
        lbl.setBounds(30, 130, 100, 15);
        add(lbl);

        edPass = new JPasswordField(Params.getParams().svnPass);
        edPass.setBounds(30, 150, 120, 20);
        add(edPass);

        updateButtons();
    }

    private void updateButtons()
    {
        edUser.setEnabled(!cbAnonymous.isSelected());
        edPass.setEnabled(!cbAnonymous.isSelected());
        getWizardFrame().enableButton(WizardFrame.BUTTON_BACK, false);
        getWizardFrame().enableButton(WizardFrame.BUTTON_FINISH, false);

        boolean canNext = true;

        if (cbRepo.getSelectedItem() != null)
            if (((String) cbRepo.getSelectedItem()).trim().length() == 0)
                canNext = false;

        if (!cbAnonymous.isSelected())
        {
            if (edUser.getText().trim().length() == 0)
                canNext = false;
        }

        getWizardFrame().enableButton(WizardFrame.BUTTON_NEXT, canNext);
    }

    public void actionPerformed(ActionEvent e)
    {
        updateButtons();
    }


    public void onNext()
    {
        getWizSettings().svnUser = edUser.getText().trim();
        getWizSettings().svnPass = new String(edPass.getPassword());
        getWizSettings().svnAuth = !cbAnonymous.isSelected();
        getWizSettings().svnRepo = (String) cbRepo.getSelectedItem();

        Params.getParams().svnUser = getWizSettings().svnUser;
        Params.getParams().svnPass = getWizSettings().svnPass;
        Params.getParams().svnAuth = getWizSettings().svnAuth;

        boolean found = false;
        String s = ((String) cbRepo.getSelectedItem()).trim();
        for (int i = 0; i < Params.getParams().svnRepo.length; i++)
            if (Params.getParams().svnRepo[i].equals(s))
                found = true;
        if (!found)
        {
            String[] temp = new String[Params.getParams().svnRepo.length + 1];
            for (int i = 0; i < Params.getParams().svnRepo.length; i++)
                temp[i] = Params.getParams().svnRepo[i];
            temp[temp.length - 1] = s;
            Params.getParams().svnRepo = temp;
        }

        Params.saveParams();

        getWizardFrame().pushWizard(new WizardRepo(getWizardFrame()));
    }

    public void changedUpdate(DocumentEvent e)
    {
        updateButtons();
    }

    public void insertUpdate(DocumentEvent e)
    {
        updateButtons();
    }

    public void removeUpdate(DocumentEvent e)
    {
        updateButtons();
    }

    public void stateChanged(ChangeEvent e)
    {
        updateButtons();
    }
}
